package org.light.shiroauth.verb;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.light.core.Verb;
import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.Dropdown;
import org.light.domain.Field;
import org.light.domain.JavascriptBlock;
import org.light.domain.JavascriptMethod;
import org.light.domain.Method;
import org.light.domain.Statement;
import org.light.domain.StatementList;
import org.light.domain.Type;
import org.light.easyui.EasyUIPositions;
import org.light.exception.ValidateException;
import org.light.utils.StringUtil;
import org.light.utils.WriteableUtil;
import org.light.verb.FindByName;

public class FindMyProfileUser extends Verb implements EasyUIPositions {

	@Override
	public Method generateDaoImplMethod() throws Exception {
		return null;
	}	

	@Override
	public Method generateDaoMethodDefinition() throws Exception {
		return null;
	}

	@Override
	public Method generateServiceMethodDefinition() throws Exception {
		return null;
	}

	@Override
	public Method generateServiceImplMethod() throws Exception  {
		return null;
	}

	public FindMyProfileUser(Domain domain,Set<Field> deniedFields) throws ValidateException{
		super();
		this.domain = domain;
		this.deniedFields = deniedFields;
		this.setVerbName("FindMyProfile"+StringUtil.capFirst(this.domain.getStandardName()));
		this.setLabel("获取用户资料");
		if  (domain.getLanguage().equalsIgnoreCase("english"))  this.setLabel("Find My Profile User");
	}

	@Override
	public Method generateControllerMethod() throws Exception {
		if (this.denied) return null;
		else {
			Method method = new Method();
			method.setStandardName(StringUtil.lowerFirst(this.getVerbName()));
			method.setReturnType(new Type("Map<String,Object>"));
			method.setThrowException(true);
			method.addAdditionalImport(this.domain.getPackageToken()+ "."+this.domain.getDomainSuffix()+"."+this.domain.getCapFirstDomainNameWithSuffix());
			method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getServiceSuffix()+"."+this.domain.getStandardName()+"Service");
			method.addAdditionalImport("org.apache.shiro.SecurityUtils");
			method.addAdditionalImport("org.apache.shiro.session.Session");
			method.addMetaData("RequestMapping(value = \"/"+StringUtil.lowerFirst(this.getVerbName())+"\", method = RequestMethod.POST)");
	
			List<Writeable> sList = new ArrayList<Writeable>();
			sList.add(new Statement(1000L,2,"Map<String,Object> result = new TreeMap<String,Object>();"));
			sList.add(new Statement(2000L,4,"Session session  = SecurityUtils.getSubject().getSession();"));
			sList.add(new Statement(3000L,2,""+this.domain.getCapFirstDomainNameWithSuffix()+" "+this.domain.getLowerFirstDomainName()+" = ("+this.domain.getCapFirstDomainNameWithSuffix()+")session.getAttribute(\"user\");"));
			sList.add(new Statement(4000L,2,"if("+this.domain.getLowerFirstDomainName()+"!=null&&!StringUtil.isBlank("+this.domain.getLowerFirstDomainName()+".get"+this.domain.findFieldByFixedName("userName").getCapFirstFieldName()+"())) {"));
			FindByName find = new FindByName(this.domain);
			sList.add(new Statement(5000L,3,""+this.domain.getLowerFirstDomainName()+" = service."+StringUtil.lowerFirst(find.getVerbName())+"("+this.domain.getLowerFirstDomainName()+".get"+this.domain.findFieldByFixedName("userName").getCapFirstFieldName()+"());"));
			sList.add(new Statement(6000L,3,"result.put(\"success\",true);"));
			sList.add(new Statement(7000L,3,"result.put(\"rows\","+this.domain.getLowerFirstDomainName()+");"));
			sList.add(new Statement(8000L,3,"return result;"));
			sList.add(new Statement(9000L,2,"}"));
			sList.add(new Statement(10000L,2,"result.put(\"success\",false);"));
			sList.add(new Statement(11000L,2,"result.put(\"rows\",null);"));
			sList.add(new Statement(12000L,2,"return result;"));
			method.setMethodStatementList(WriteableUtil.merge(sList));		
			return method;
		}
	}	

	@Override
	public JavascriptBlock generateEasyUIJSButtonBlock() throws Exception {
		return null;
	}

	@Override
	public JavascriptMethod generateEasyUIJSActionMethod() throws Exception {
		if (this.denied) return null;
		else {
			Domain domain = this.domain;
			JavascriptMethod method = new JavascriptMethod();
			method.setSerial(200);
			method.setStandardName(StringUtil.lowerFirst(this.getVerbName()));
			
			List<Writeable> sList = new ArrayList<Writeable>();
			long serial = 1000L;
			sList.add(new Statement(serial+1000L,1,"$.ajax({"));
			sList.add(new Statement(serial+2000L,2,"type: \"post\","));
			sList.add(new Statement(serial+3000L,2,"url: \"../"+ this.domain.getControllerPackagePrefix()+"profile"+this.domain.getControllerNamingSuffix()+"/"+StringUtil.lowerFirst(this.getVerbName())+"\","));
			sList.add(new Statement(serial+4000L,2,"dataType: 'json',"));
			sList.add(new Statement(serial+5000L,2,"success: function(data, textStatus) {"));
			sList.add(new Statement(serial+6000L,3,"if (data.success) {"));
			sList.add(new Statement(serial+7000L,4,"var rows = data.rows;"));
			sList.add(new Statement(serial+8000L, 4,
					"$(\"#ff\").find(\"#" + this.domain.getDomainId().getLowerFirstFieldName()
							+ "\").val(rows[\"" + this.domain.getDomainId().getLowerFirstFieldName() + "\"]);"));
			serial += 9000L;
			for (Field f : domain.getFieldsWithoutIdAndActive()) {
				if (!this.deniedFields.contains(f)) {
					if (f.getFieldType().equalsIgnoreCase("image")) {
						sList.add(new Statement(serial, 4, "if (!isBlank(rows[\""+f.getLowerFirstFieldName()+"\"]))	{"));
						sList.add(new Statement(serial+10, 5, "$(\"#ff\").find(\"#"+f.getLowerFirstFieldName()+"\").prop(\"src\",\"data:image/png;base64,\"+rows[\""+f.getLowerFirstFieldName()+"\"]);"));
						sList.add(new Statement(serial+20, 4, "}else{"));
						sList.add(new Statement(serial+30, 5, "$(\"#ff\").find(\"#"+f.getLowerFirstFieldName()+"\").prop(\"src\",\"../images/blank.jpg\");"));
						sList.add(new Statement(serial+40, 4, "}"));
					}else if (f instanceof Dropdown) {
						sList.add(new Statement(serial, 4, "$(\"#ff\").find(\"#" + f.getLowerFirstFieldName()
								+ "\").combobox(\"setValue\",rows[\"" + f.getLowerFirstFieldName() + "\"]);"));
					} else {
						if (f.getFieldType().equalsIgnoreCase("boolean")) {
							sList.add(new Statement(serial, 4,
									"var " + f.getLowerFirstFieldName() + "Checkboxs = $(\"#ff\").find(\"input[name='"
											+ f.getLowerFirstFieldName() + "']\");"));
							sList.add(new Statement(serial + 100, 4,
									"for (var i=0;i<" + f.getLowerFirstFieldName() + "Checkboxs.length;i++){"));
							sList.add(new Statement(serial + 200, 5,
									"if (" + f.getLowerFirstFieldName() + "Checkboxs.get(i).value == \"\"+rows[\""
											+ f.getLowerFirstFieldName() + "\"]) " + f.getLowerFirstFieldName()
											+ "Checkboxs.get(i).checked=true;"));
							sList.add(new Statement(serial + 500, 4, "}"));
						} else if (f.isTextarea()) {
							sList.add(new Statement(serial, 4, "$(\"#ff\").find(\"#" + f.getLowerFirstFieldName()
									+ "\").val(rows[\"" + f.getLowerFirstFieldName() + "\"]);"));
						} else {
							sList.add(new Statement(serial, 4, "$(\"#ff\").find(\"#" + f.getLowerFirstFieldName()
									+ "\").textbox(\"setValue\",rows[\"" + f.getLowerFirstFieldName() + "\"]);"));
						}
					}
					serial += 1000;
				}
			}
			sList.add(new Statement(serial, 4, "var checkboxs = $(\"#ff\").find(\"input[name='"
					+ domain.getActive().getLowerFirstFieldName() + "']\");"));
			sList.add(new Statement(serial + 100, 4, "for (var i=0;i<checkboxs.length;i++){"));
			sList.add(new Statement(serial + 200, 5, "if (checkboxs.get(i).value == \"\"+rows[\""
					+ domain.getActive().getLowerFirstFieldName() + "\"]) checkboxs.get(i).checked=true;"));
			
			sList.add(new Statement(serial+40000L,4,"}"));
			sList.add(new Statement(serial+40000L,3,"}"));
			sList.add(new Statement(serial+41000L,2,"},"));
			sList.add(new Statement(serial+42000L,2,"complete : function(XMLHttpRequest, textStatus) {"));
			sList.add(new Statement(serial+43000L,2,"},"));
			sList.add(new Statement(serial+44000L,2,"error : function(XMLHttpRequest,textStatus,errorThrown) {"));
			sList.add(new Statement(serial+45000L,3,"alert(\"Error:\"+textStatus);"));
			sList.add(new Statement(serial+46000L,3,"alert(errorThrown.toString());"));
			sList.add(new Statement(serial+47000L,2,"}"));
			sList.add(new Statement(serial+48000L,1,"});"));
			
			method.setMethodStatementList(WriteableUtil.merge(sList));
			return method;

		}
	}

	@Override
	public Method generateDummyDaoImplMethod() throws Exception {
		return null;
	}
}
